package com.lancoo.edu.platform.operation.commons.service.bo;

import cn.hutool.core.collection.CollectionUtil;

import com.lancoo.edu.platform.operation.commons.domain.api.application.QueryIdentityModuleApiResp;
import com.lancoo.edu.platform.operation.commons.domain.api.system.ProductInfo;
import com.lancoo.edu.platform.operation.commons.domain.api.user.OnlineUserInfo;
import com.lancoo.edu.platform.operation.commons.service.api.UserInfoApiService;
import com.lancoo.edu.platform.operation.commons.config.LancooConfig;
import com.lancoo.edu.platform.operation.commons.domain.vo.IdentityVo;
import com.lancoo.edu.platform.operation.commons.domain.vo.UserInfoVo;
import com.lancoo.edu.platform.operation.commons.exception.ServiceException;
import com.lancoo.edu.platform.operation.commons.exception.TokenException;
import com.lancoo.edu.platform.operation.commons.utils.ListUtil;

import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;


/**
 * 用户信息【响应】业务逻辑
 * @author pky
 */
@Service
@AllArgsConstructor
public class UserInfoBoService {

    private final UserInfoApiService userInfoApiService;
    private final LancooConfig lancooConfig;

    /**
     * 通过 token 获取登录信息
     *
     * @param token 令牌
     * @return 登录信息
     */
    public UserInfoVo getByToken(String token) throws UnsupportedEncodingException {
        // 1、从基础平台获取用户信息
//        UserInfo userInfo = userInfoApiService.getUserInfo(token);
        OnlineUserInfo userInfo = userInfoApiService.getOnlineUserInfo(token);
        if (userInfo == null) {
            throw new ServiceException("用户信息不存在");
        }
        // 2、获取用户身份信息
//        UserIdentity identityInfo = userInfoApiService.getIdentityInfo(token);
        String userId = userInfo.getUserId();
        List<QueryIdentityModuleApiResp> identityInfoList = userInfoApiService.getIdentityModule(userId);
        // 3、获取产品初始化配置信息
        ProductInfo productInfo = userInfoApiService.getProductInfo();
        // 4、设置返回的登录参数
        UserInfoVo userInfoVo = ListUtil.copy(userInfo, UserInfoVo.class);
        userInfoVo.setUserName(URLDecoder.decode(userInfo.getUserName(), "utf-8"));
        userInfoVo.setGender(URLDecoder.decode(userInfo.getGender(), "utf-8"));
        userInfoVo.setPhotoPath(URLDecoder.decode(userInfo.getPhotoPath(), "utf-8"));

        if(productInfo != null) {
            userInfoVo.setProductUseRange(productInfo.getProductUseRange());
        }
        // 5、通过用户ID获取身份信息
//        List<UserIdentity> identityInfoList = identityApiService.getUserIdentityInfo(userInfo.getUserId(), token);
        userInfoVo.setIdentityList(ListUtil.copy(identityInfoList, IdentityVo.class));
        userInfoVo.setBaseUrl(lancooConfig.getBaseUrl());
        userInfoVo.setSysId(LancooConfig.SYS_ID);
        // 5.1、设置用户身份名identityName
        if(CollectionUtil.isEmpty(identityInfoList)){
            return userInfoVo;
        }
//        Map<String, List<UserIdentity>> identityInfoMap = identityInfoList.stream().collect(Collectors.groupingBy(UserIdentity::getIdentityCode));
//        Map<String, List<QueryIdentityModuleApiResp>> identityInfoMap = identityInfoList.stream().collect(Collectors.groupingBy(QueryIdentityModuleApiResp::getIdentityCode));
//        String identityName = identityInfoMap.getOrDefault(userInfoVo.getIdentityCode(), null).get(0).getIdentityName();
        String identityName = identityInfoList.get(0).getIdentityName();
        userInfoVo.setIdentityName(identityName);
        userInfoVo.setIdentityCode(identityInfoList.get(0).getIdentityCode());
        return userInfoVo;
    }


    /**
     * 根据 Token 判断用户信息
     *
     * @param token 令牌
     */
    public void checkByToken(String token)  {
        // 执行认证
        if (StringUtils.isBlank(token)) {
            throw new TokenException();
        }
        // 基础平台获取用户信息
//        UserInfo userInfo = userInfoApiService.getUserInfo(token);
        OnlineUserInfo userInfo = userInfoApiService.getOnlineUserInfo(token);
        if (userInfo == null || StringUtils.isBlank(userInfo.getUserId())) {
            throw new TokenException();
        }
    }

//     /**
//      * 我的讨论组中搜索功能
//      * @param eduId 教育局ID
//      * @param keyword 关键词(用户名/用户ID)
//      * @param groupId 讨论组ID
//      * @return 人员信息
//      */
//     public List<SelectObjectUserVo> searchByContactGroup(String eduId, String keyword, String groupId) {
//         // 1、搜索所有满足关键词的人员信息
// //        List<SelectObjectUserVo> userVoList = getAllUser(schoolId,null, keyword, schoolLevel);
//         List<SelectObjectUserVo> userVoList = selectObjectBoService.searchAllUser(eduId,null, keyword);
//         // 2、查询指定讨论组中人员信息
//         ContactGroup contactGroup = contactGroupService.getContactGroup(groupId);
//         // 3、呈现给前端数据
//         userVoList.forEach(userVo -> {
//             if (contactGroup.getPeopleIds().contains(userVo.getUserId())) {
//                 userVo.setStatus(1);
//             }
//         });
//         return userVoList;
//     }
}
